package com.atguigu.yygh.apartment.controller.api;

import com.atguigu.yygh.apartment.service.ApartmentInfoService;
import com.atguigu.yygh.apartment.service.ApartmentService;
import com.atguigu.yygh.apartment.service.ChamberService;
import com.atguigu.yygh.apartment.service.StudentInfoService;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.model.hosp.ApartmentInfo;
import com.atguigu.yygh.model.hosp.Chamber;
import com.atguigu.yygh.model.hosp.StudentInfo;
import com.atguigu.yygh.vo.hosp.ApartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ChamberQueryVo;

import com.atguigu.yygh.vo.hosp.StudentInfoQueryVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/apartment")
public class ApiController {
    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private ApartmentService apartmentService;

    @Autowired
    private ChamberService chamberService;

    @Autowired
    private StudentInfoService studentInfoService;


    //查询学生接口
    @PostMapping("studentInfo/list")
    public Result studentInfoList(HttpServletRequest request){
        //获取传递过来的寝室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取宿舍楼编号
        String apartmentCode = (String) paramMap.get("apartmentCode");

        //寝室编号
        String chamberCode = (String) paramMap.get("chamberCode");
        //当前页与记录数
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
        //签名校验
        //获取系统传递过来的签名
        String sign = (String) paramMap.get("sign"); //加密后

        //根据传递过来的编码，查询数据库，查询签名
        String signKey = apartmentService.getSignKey(apartmentCode);

        //把数据查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        StudentInfoQueryVo studentInfoQueryVo = new StudentInfoQueryVo();

        studentInfoQueryVo.setApartmentCode(apartmentCode);
        studentInfoQueryVo.setChamberCode(chamberCode);

        //调用service方法
        Page<StudentInfo> pageModel = studentInfoService.findPageStudentInfo(page,limit,studentInfoQueryVo);


        return Result.ok(pageModel);
    }


    //删除寝室接口
    @PostMapping("chamber/remove")
    public Result removeChamber(HttpServletRequest request){
        //获取传递过来的寝室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //宿舍编号，寝室编号
        String apartmentCode = (String) paramMap.get("apartmentCode");
        String chamberCode = (String) paramMap.get("chamberCode");
        System.out.println("ChamberCode:"+chamberCode);
        System.out.println("apartmentCode:"+apartmentCode);
        //签名校验
        //获取系统传递过来的签名
        String sign = (String) paramMap.get("sign"); //加密后

        //根据传递过来的编码，查询数据库，查询签名
        String signKey = apartmentService.getSignKey(apartmentCode);

        //把数据查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法
        chamberService.remove(apartmentCode,chamberCode);
        return Result.ok();
    }

    //查询寝室接口
    @PostMapping("chamber/list")
    public Result findChamber(HttpServletRequest request){
        //获取传递过来的寝室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取宿舍楼编号
        String apartmentCode = (String) paramMap.get("apartmentCode");
        //当前页与记录数
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
        //签名校验
            //获取系统传递过来的签名
            String sign = (String) paramMap.get("sign"); //加密后

            //根据传递过来的编码，查询数据库，查询签名
            String signKey = apartmentService.getSignKey(apartmentCode);

            //把数据查询出来的签名进行MD5加密
            String signKeyMd5 = MD5.encrypt(signKey);

            //判断签名是否一致
            if (!sign.equals(signKeyMd5)) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
        ChamberQueryVo chamberQueryVo = new ChamberQueryVo();
            chamberQueryVo.setApartmentCode(apartmentCode);
        //调用service方法
        Page<Chamber> pageModel = chamberService.findPageChamber(page,limit,chamberQueryVo);


        return Result.ok(pageModel);
    }

    //上传寝室接口
    @PostMapping("saveChamber")
    public Result saveChamber(HttpServletRequest request){
        //获取传递过来的寝室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取传递过来的宿舍编号
        String apartmentCode = (String) paramMap.get("apartmentCode");

        //获取系统传递过来的签名
        String sign = (String) paramMap.get("sign"); //加密后

        //根据传递过来的编码，查询数据库，查询签名
        String signKey = apartmentService.getSignKey(apartmentCode);

        //把数据查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法
        chamberService.save(paramMap);
        return Result.ok();

    }

    //查询宿舍接口
    @PostMapping("apartmentInfo/show")
    public Result getApartmentInfo(HttpServletRequest request){
        //获取传递过来的宿舍信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取传递过来的宿舍编号
        String apartmentCode = (String) paramMap.get("apartmentCode");

        //获取系统传递过来的签名
        String sign = (String) paramMap.get("sign"); //加密后

        //根据传递过来的编码，查询数据库，查询签名
        String signKey = apartmentService.getSignKey(apartmentCode);

        //把数据查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据宿舍编号查询
        ApartmentInfo apartmentInfo = apartmentInfoService.getByApartmentCode(apartmentCode);
        return Result.ok(apartmentInfo);
    }

    //上传宿舍接口
    @PostMapping("saveApartmentInfo")
    public Result saveApartmentInfo(HttpServletRequest request){
        //获取传递过来的宿舍信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        System.out.println("-----------------------------------------------------");
        System.out.println("paramMap:"+paramMap.get("sign"));
        //获取系统传递过来的签名
        String sign = (String) paramMap.get("sign"); //加密后

        //根据传递过来的编码，查询数据库，查询签名
        String apartmentCode = (String) paramMap.get("apartmentCode");
        String signKey = apartmentService.getSignKey(apartmentCode);

        //把数据查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中"+" 转换为了" ",转换
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);
        //调用service方法
        apartmentInfoService.save(paramMap);
        return Result.ok();
    }
    //上传学生接口
    @PostMapping("saveStudentInfo")
    public Result saveStudentInfo(HttpServletRequest request){
        //获取传递过来的学生信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取传递过来的宿舍编号
        String apartmentCode = (String) paramMap.get("apartmentCode");

        //获取系统传递过来的签名
        String sign = (String) paramMap.get("sign"); //加密后

        //根据传递过来的编码，查询数据库，查询签名
        String signKey = apartmentService.getSignKey(apartmentCode);

        //把数据查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法
        studentInfoService.save(paramMap);
        return Result.ok();
    }

    //删除寝室接口
    @PostMapping("studentInfo/remove")
    public Result removeStudentInfo(HttpServletRequest request){
        //获取传递过来的寝室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //宿舍编号，寝室编号
        String apartmentCode = (String) paramMap.get("apartmentCode");
        String bedId = (String) paramMap.get("bedId");

        //签名校验
        //获取系统传递过来的签名
        String sign = (String) paramMap.get("sign"); //加密后

        //根据传递过来的编码，查询数据库，查询签名
        String signKey = apartmentService.getSignKey(apartmentCode);

        //把数据查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法
        studentInfoService.remove(apartmentCode,bedId);
        return Result.ok();
    }



}
